package cn.net.lwj.singledb.service.impl;

import cn.net.lwj.common.service.Impl.BaseServiceImpl;
import cn.net.lwj.singledb.service.TXfTaskService;
import generate.entity.TSjjhLog;
import generate.entity.TXfBljdqk;
import generate.entity.TXfTask;
import generate.mapper.TSjjhLogMapper;
import generate.mapper.TXfBljdqkMapper;
import generate.mapper.TXfTaskMapper;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 测试事务，在类上加@Transactional
 */
@Service
@Transactional
@Slf4j
public class DemoServiceImpl extends BaseServiceImpl {

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    @Resource
    private TSjjhLogMapper sjjhLogMapper;
    @Resource
    private TXfBljdqkMapper bljdqkMapper;
    @Resource
    private TXfTaskMapper taskMapper;
    @Resource
    private TXfTaskService taskService;

    @Resource
    private TransactionalServiceImpl transactionalService;
    /**
     * 测试无事务行为
     */
    public void testNoTx(){
        //List<Map<String, Object>> list = sqlSessionTemplate.getMapper(TreeMapper.class).getQgfyTree("");
        TXfTask task = new TXfTask("1", "1", 1004);
        taskService.saveOrUpdate(task);
        "".substring(5);
        //taskMapper.select();
        List<TXfTask> list = taskService.list();
        TXfBljdqk bljdqk = new TXfBljdqk("1", 1006, "", "", new Date());
        bljdqkMapper.insert(bljdqk);
        //如果此处抛出异常，上面的保存不会回滚,类或者方法上有事务注解则被回滚
        "".substring(5);
        System.out.println(list);
        log.info("222222222");
    }




    @Transactional
    public void testTx1(){
        //List<Map<String, Object>> list = sqlSessionTemplate.getMapper(TreeMapper.class).getQgfyTree("");
        //System.out.println(list);
        TXfTask task = new TXfTask("1", "1", 1004);
        //taskService.saveOrUpdate(task);
        //taskMapper.select();
        List<TXfTask> list = taskService.list();
        TXfBljdqk bljdqk = new TXfBljdqk("1", 1006, "", "", new Date());
        bljdqkMapper.insert(bljdqk);
        "".substring(5);
        System.out.println(list);
        log.info("222222222");
    }

    //@Transactional
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    //@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void testTx2(){
        //List<Map<String, Object>> list = sqlSessionTemplate.getMapper(TreeMapper.class).getQgfyTree("");
        //System.out.println(list);
        TXfTask task = new TXfTask("1", "1", 1004);
        //taskService.saveOrUpdate(task);
        //taskMapper.select();
        List<TXfTask> list = taskService.list();
        TXfBljdqk bljdqk = new TXfBljdqk("1", 1006, "", "", new Date());
        bljdqkMapper.insert(bljdqk);
        "".substring(5);
        System.out.println(list);
        log.info("222222222");
    }

    @Transactional
    public void testTx3(){
        //List<Map<String, Object>> list = sqlSessionTemplate.getMapper(TreeMapper.class).getQgfyTree("");
        //System.out.println(list);
        TXfTask task = new TXfTask("1", "1", 1004);
        //taskService.saveOrUpdate(task);
        //taskMapper.select();
        List<TXfTask> list = taskService.list();
        TXfBljdqk bljdqk = new TXfBljdqk("1", 1006, "", "", new Date());
        bljdqkMapper.insert(bljdqk);
        "".substring(5);
        System.out.println(list);
        log.info("222222222");
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void saveLogTxNew(TSjjhLog log) {
        sjjhLogMapper.insert(log);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void saveLogTx(TSjjhLog log) {
        sjjhLogMapper.insert(log);
    }

    public void saveLog(TSjjhLog log) {
        sjjhLogMapper.insert(log);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void saveBljdqkTxNew(TXfBljdqk bljdqk) {
        bljdqkMapper.insert(bljdqk);
    }
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void saveBljdqkTx(TXfBljdqk bljdqk) {
        bljdqkMapper.insert(bljdqk);
    }
    public void saveBljdqk(TXfBljdqk bljdqk) {
        bljdqkMapper.insert(bljdqk);
    }
}
